using UnityEngine;

public class PlayerAIState_AfterBaseLine : State<PlayerAI>
{
	public static PlayerAIState_AfterBaseLine instance;

	private Vector3 targetPos;

	private PlayerAIState_AfterBaseLine()
	{
	}

	public static PlayerAIState_AfterBaseLine getInstance()
	{
		if (instance == null)
		{
			instance = new PlayerAIState_AfterBaseLine();
		}
		return instance;
	}

	public override void enter(PlayerAI owner)
	{
		base.enter(owner);
		getTargetPos(owner);
		if (owner.player.IsTeamCrossHalfCourt())
		{
			owner.do_offense_findpos();
		}
	}

	public override void fixedUpdate(PlayerAI owner)
	{
		base.fixedUpdate(owner);
		if (!owner.player.IsTeamCrossHalfCourt())
		{
			if (MathHelper.getSqrDistance(owner.player.getPosition(), targetPos) < 4f)
			{
				if (GameHelper.MoveTo(owner.player, targetPos, owner.player.getMoveSpeed(), isLookAtTargetPos: false))
				{
					Vector3 position = owner.player.getPosition();
					float z = position.z;
					Vector3 position2 = getTargetPlayer(owner).getPosition();
					if (!(Mathf.Abs(z - position2.z) < 1f))
					{
						Vector3 position3 = owner.player.getPosition();
						float z2 = position3.z;
						Vector3 position4 = getTargetPlayer(owner).getPosition();
						if (!(Mathf.Abs(z2 - position4.z) > 5f))
						{
							owner.player.do_idle();
							owner.player.LookAtSmooth(getTargetPlayer(owner).getPosition());
							goto IL_0105;
						}
					}
					getTargetPos(owner);
				}
				else
				{
					owner.player.do_run();
				}
				goto IL_0105;
			}
			if (GameHelper.MoveTo(owner.player, targetPos, owner.player.getMoveSpeed(), isLookAtTargetPos: true))
			{
				Vector3 position5 = owner.player.getPosition();
				float z3 = position5.z;
				Vector3 position6 = getTargetPlayer(owner).getPosition();
				if (!(Mathf.Abs(z3 - position6.z) < 1f))
				{
					Vector3 position7 = owner.player.getPosition();
					float z4 = position7.z;
					Vector3 position8 = getTargetPlayer(owner).getPosition();
					if (!(Mathf.Abs(z4 - position8.z) > 5f))
					{
						owner.player.do_idle();
						owner.player.LookAt(getTargetPlayer(owner).getPosition());
						return;
					}
				}
				getTargetPos(owner);
			}
			else
			{
				owner.player.do_run();
			}
			return;
		}
		owner.do_offense_findpos();
		return;
		IL_0105:
		owner.player.LookAtSmooth(getTargetPlayer(owner).getPosition());
	}

	private void getTargetPos(PlayerAI owner)
	{
		if (owner.player.getOffenseDirection() == OffenseDirection.RIGHT)
		{
			float x = Random.Range((0f - Global.rectCourt.height) * 0.5f, Global.rectCourt.height * 0.5f);
			Vector3 position = getTargetPlayer(owner).getPosition();
			targetPos = new Vector3(x, 0f, position.z - (float)Random.Range(3, 5));
		}
		else
		{
			float x2 = Random.Range((0f - Global.rectCourt.height) * 0.5f, Global.rectCourt.height * 0.5f);
			Vector3 position2 = getTargetPlayer(owner).getPosition();
			targetPos = new Vector3(x2, 0f, position2.z + (float)Random.Range(3, 5));
		}
	}

	private Player getTargetPlayer(PlayerAI owner)
	{
		if (owner.player.teamType == TeamType.COMPUTER)
		{
			if (!owner.player.team.IsAnyOneControlBall())
			{
				return owner.player.team.playerPG;
			}
			return owner.player.team.getPlayer_ControlBall();
		}
		return owner.player.team.getPlayer_ControlBall();
	}
}
